package com.atguigu.gulimall.seckill.scheduled;

import com.atguigu.gulimall.seckill.Service.SeckillService;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;




@Slf4j
@Service
public class SeckillskuScheduled {

    @Autowired
    RedissonClient redissonClient;

    @Autowired
    SeckillService seckillService;

    private final String  upload_lock = "seckill:upload:lock";

    //TODO 幂等性处理，上架过的商品就不用再上架了

//    @Scheduled(cron = " 0 * * * * ?")
    public void uploadSecKillsLastest3Days(){
        log.info("上架秒杀的信息");
        //TODO 上架商品的锁。防止两个进程同时进来，上架了两份
        RLock lock = redissonClient.getLock(upload_lock);
        lock.lock(10, TimeUnit.SECONDS);
        try{
            seckillService.uploadSecKillsLastest3Days();
        }finally {
            lock.unlock();
        }
    }

}
